import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import linregress

# 读取上传的CSV文件
df = pd.read_csv('data/data_能巨.csv')

# 提取两列数据
x = df['pulse_count'].values
y = df['gas_flow'].values

# 使用线性回归拟合数据
slope, intercept, r_value, p_value, std_err = linregress(x, y)

# 构建函数表达式
function_expression = f"y = {slope:.6f} * x + {intercept:.6f}"

print(f"a:{slope}")
print(f"b:{intercept}")
print(function_expression)

# 创建拟合曲线的预测值
fit = slope * x + intercept

# 绘制原始数据和拟合曲线
plt.figure(figsize=(10, 6))
plt.scatter(x, y, label='Original data', color='blue')
plt.plot(x, fit, color='red', label='Fitted line')

# 添加图例和标题
plt.legend()
plt.title('Gas Flow vs Pulse Count')
plt.xlabel('Pulse Count')
plt.ylabel('Gas Flow')

# 显示图表
plt.grid(True)
# plt.show()
plt.savefig('python/linear_regression_fitting_fun.jpg')
